$(function(){


	function GetQueryString (name) { //获取指定name的url参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return (r[2]);
        return null;
    }
    function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },2000)
    }


    const userId = GetQueryString('userId');
    const token = GetQueryString('token');
    const host =  'http://'+GetQueryString('invokeDomain');


    //初使化
    let loadIng = true,pageNum = 1;
    function info(pageNum) {
        if (!loadIng) { return false};
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/transfer/moneyLogs',
            data: {
                'userId':userId,
                'token':token,
                'pageNum':pageNum,
                'pageSize':10
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data;
                if (code == 0) {
                    loadIng = (data&&data.length==10)?true:false;
                    if (data&&data.length>0) {
                        data.forEach(function(item,index){
                            let html = '<li><div class="left"><p>'+item.logDesc+'</p></div>'+
                                        '<div class="rigth">'+
                                        '   <p>'+item.money+'红包券</p>'+
                                        '    <span>'+item.createTime+'</span>'+
                                        '</div></li>';
                            $('.main').append(html);
                        })
                    }

                    //如果加载第一页后，页面内容没有加载满，可以再加载一次
                    if (!loadIng) {return false}
                    var scrollTop = $(window).scrollTop();
                    var scrollHeight = $('body').height();
                    var windowHeight = $(window).height();
                    if (scrollTop + windowHeight  >= scrollHeight ) {
                   　　 pageNum++;
                        info(pageNum);
                    }
                }else{
                    Toast(msg);
                }
                scrollState = true
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
                scrollState = true
            }
        })
    }
    info(1);
    let scrollState = true;
    $(window).scroll(function () {
        if (!scrollState) {return false}
        scrollState = false;
        if (!loadIng) { return false};
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $('body').height();
        var windowHeight = $(this).height();
        var topHeight = $('.header').height();
        if (scrollTop + windowHeight + topHeight  >= scrollHeight) {
       　　 pageNum++;
            info(pageNum);
        }
    });
})